<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.app
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       cake
 * @subpackage    cake.app
 */
class AppController extends Controller {
	var $helpers = array('Html', 'Form', 'Session', 'Js','Javascript');
    var $components = array('Session','Auth','Cookie');
    var $uses = array('User');
    function beforeFilter(){
        	$this->Auth->authenticate = ClassRegistry::init('User');
            $user = $this->Auth->user();
            if(empty($user) && $this->params['action'] != 'login'){
                $this->redirect(array('controller' => 'users', 'action' => 'login'));
        }
       	$this->Auth->loginAction 	= array('controller' => 'users', 'action' => 'login');
		$this->Auth->loginRedirect 	= array('controller'=>'pages', 'action' => 'home');
		$this->Auth->loginError 	= 'Tài kho?n ho?c m?t kh?u sai';
		$this->Auth->authError 		= 'B?n ph?i dang nh?p m?i có th? s? d?ng ch?c nang';
		$this->Auth->flashElement = 'autherr';
		$this->Auth->logoutRedirect = '/';
      
    }
    
    function beforeRender() {
		
        $curUser=$this->Auth->user();
        if(count($curUser)>0){
            $user=$this->User->find('first', array('conditions'=>array('User.id'=>$curUser['User']['id']),'recursive'=>1));
            $cur_role=$user['User'] ['role'];
           $this->Session->write( 'role_id',$cur_role);
           switch ($cur_role){
			case 1 :
                    $canbo=$this->User->query('select giangviens.* from giangviens , users
                                                where users.username = giangviens.msgv
                                                and users.id='.$curUser['User']['id']);
                                             
                                         
            break;
            case 2 :
                    $sinhvien=$this->User->query('select sinhviens.* from sinhviens , users
                                                where users.username = sinhviens.mssv
                                                and users.id='.$curUser['User']['id']);
                                                
            break;
            
        }
        $this->set(compact('cur_role','canbo','sinhvien','role_id','curUser'));
    }
   }
	
/**
 * nganhs
 * @var Nganh
 */
	var $Nganh;
/**
 * sinhviens
 * @var Sinhvien
 */
	var $Sinhvien;
	
/**
 * Khoa
 * @var Khoa
 */
	var $Khoa;
/**
 * Monthamgianhom
 * @var Monthamgianhom
 */
	
	var $Monthamgianhom;
	/**
 * Ctdt
 * @var Ctdt
 */
var $Ctdt;
	/**
 * NhomHp
 * @var Nhomhp
 */
var $Nhomhp;


/**
 * Lop
 * lop
 * @var Lop
 */
var $Lop;

/**
 * Lophp
 * lophp
 * @var Lophp
 */
var $Lophp;

/**
 * Mon
 * mon
 * @var Mon
 */
var $Mon;

/**
 * Nkhk
 * nkhk
 * @var Hknk
 */
var $Hknk;

/**
 * Ketqua
 * Ketqua
 * @var Ketqua
 */
var $Ketqua;

/**
 * Session
 * @var SessionComponent
 */
var $Session;
/**
 * RequestHandler
 * @var RequestHandlerComponent
 */
var $RequestHandler;

/**
 * @var AuthComponent
 */
	var $Auth;
}